﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Threading;

namespace C1WijmoProManagement
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static string connectionStr = System.Configuration.ConfigurationManager.ConnectionStrings["datasource"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.orderPro.SelectedIndex = 0;
                this.orderRegion.SelectedIndex = 0;
                this.orderDate.Date = DateTime.Now;
                this.orderRemark.Text = "暂无备注。";
                this.orderManager.Mask = "";
                return;

            }
        }

        protected void orderSubmit_Click(object sender, EventArgs e)
        {
            OleDbParameter date = new OleDbParameter("@date", OleDbType.Date);
            date.Value = this.orderDate.Date;

            OleDbParameter product = new OleDbParameter("@product", OleDbType.VarChar);
            product.Value = this.orderPro.SelectedItem.Text;

            OleDbParameter num = new OleDbParameter("@num", OleDbType.Integer);
            num.Value = this.orderNO.Value;

            OleDbParameter sum = new OleDbParameter("@sum", OleDbType.Integer);
            sum.Value = this.orderSum.Value;

            OleDbParameter region = new OleDbParameter("@region", OleDbType.VarChar);
            region.Value = this.orderRegion.SelectedItem.Text;

            OleDbParameter manager = new OleDbParameter("@manager", OleDbType.VarChar);
            manager.Value = this.orderManager.Text;

            OleDbParameter remark = new OleDbParameter("@remark", OleDbType.VarChar);
            remark.Value = this.orderRemark.Text;

            //string sqlcommand = "Insert into SalesData(合同号,日期,产品,数量,收入,区域,销售经理,备注) values(@合同号,'#@date#','@product','@num','@sum','@region','@manager','@remark')";
            string sqlcommand = "Insert into SalesData(日期,产品,数量,收入,区域,销售经理,备注) values(@date,@product,@num,@sum,@region,@manager,@remark)";
            //string sqlcommand = "Insert into SalesData(合同号,日期,产品,数量,收入,区域,销售经理,备注) values('@合同号','2012-12-14','@product','@num','@sum','@region','@manager','@remark')";

            OleDbParameter[] parameters =new OleDbParameter[]{date, product, num, sum, region, manager, remark };

            if (ExecuteSql(sqlcommand, parameters)!=0)
            {
                Response.Write("<script>if(confirm('添加数据成功，是否转到订单页查看？')){document.location.href='gridview.aspx';}</script>");
            }

        }
        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params OleDbParameter[] cmdParms)
        {

            using (OleDbConnection connection = new OleDbConnection(connectionStr))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (System.Data.OleDb.OleDbException E)
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
        }

        private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, OleDbParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = CommandType.Text;//cmdType;
            if (cmdParms != null)
            {
                foreach (OleDbParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

    }
}